const http = require('http');

// 测试目录文件列表API
function testDirectoryAPI() {
    const options = {
        hostname: 'localhost',
        port: 3000,
        path: '/static/api/directory/SolarPower',
        method: 'GET',
        headers: {
            'Accept': 'application/json'
        }
    };

    const req = http.request(options, (res) => {
        console.log(`状态码: ${res.statusCode}`);
        console.log(`响应头:`, res.headers);
        
        let data = '';
        res.on('data', (chunk) => {
            data += chunk;
        });
        
        res.on('end', () => {
            try {
                const jsonData = JSON.parse(data);
                console.log('响应数据:');
                console.log(JSON.stringify(jsonData, null, 2));
                
                if (jsonData.files && jsonData.files.length > 0) {
                    console.log(`\n找到 ${jsonData.files.length} 个文件:`);
                    jsonData.files.forEach((file, index) => {
                        console.log(`${index + 1}. ${file.path} (${file.size} bytes)`);
                    });
                }
            } catch (e) {
                console.error('解析JSON失败:', e);
                console.log('原始响应:', data);
            }
        });
    });

    req.on('error', (e) => {
        console.error(`请求失败: ${e.message}`);
    });

    req.end();
}

// 测试静态资源访问
function testStaticResource() {
    const options = {
        hostname: 'localhost',
        port: 3000,
        path: '/static/html/SolarPower/SolarPower.html',
        method: 'GET'
    };

    const req = http.request(options, (res) => {
        console.log(`\n静态资源状态码: ${res.statusCode}`);
        console.log(`Content-Type: ${res.headers['content-type']}`);
        
        let data = '';
        res.on('data', (chunk) => {
            data += chunk;
        });
        
        res.on('end', () => {
            console.log(`文件大小: ${data.length} 字节`);
            if (data.includes('<!DOCTYPE html>')) {
                console.log('✓ HTML文件内容正常');
            } else {
                console.log('✗ HTML文件内容异常');
            }
        });
    });

    req.on('error', (e) => {
        console.error(`静态资源请求失败: ${e.message}`);
    });

    req.end();
}

console.log('开始测试服务器API...\n');

// 先测试目录API
testDirectoryAPI();

// 延迟测试静态资源
setTimeout(() => {
    testStaticResource();
}, 1000);
